<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');	 
class Verifylogin extends CI_Controller {
    function __construct()
    {
        parent::__construct();
	$this->load->model('Login_m');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('Readcsvlogin_m');
        $this->load->model('Comment_m');
    } 
    function index()
    {
        
        if($this->check_database()==0)
        {
            $data['error_login']="ユーザーまたはパースワードが違い!";
            $this->load->view('login/login_v',$data);
        }
        else if($this->check_database()==6)
        {
           $data['error_seikyuu']="契約期間がとまりました!";
           $this->load->view('login/login_v',$data); 
        }
	else if($this->check_database()==1)
	{
            $session_data_ad = $this->session->userdata('logged_in_ad');
            $id_ad = $session_data_ad['id_ad'];
            $this->Login_m->update_online_ad($id_ad,1);
            redirect('admin', 'refresh');
	}
        else if($this->check_database()==2)
        {
            $session_data_mg = $this->session->userdata('logged_in_mg');
            $id_mg = $session_data_mg['id_mg'];
            $this->Login_m->update_online_mg($id_mg,1);
            redirect('manager','refresh');
        }
    }
    function check_database()
    {
        $id = $this->input->post('txtID');
        $password = $this->input->post('txtPass');
        $session_array_id = array('id_login'=>$id);
        $this->session->set_userdata('login_id', $session_array_id);
        //query the database
        $result_ad = $this->Login_m->login_admin($id, $password); 
        $result_mg = $this->Login_m->login_manager($id, $password); 
        if($this->Login_m->select_online_flag_ad($id)->num_rows()>0||
                $this->Login_m->select_online_flag_mg($id)->num_rows()>0)
                return 0;
        if($result_ad)
        {
            $sess_array_admin = array();
            foreach($result_ad as $row_ad)
            {
                $sess_array_admin = array('id_ad' => $row_ad->id_ad);
                
                $this->session->set_userdata('logged_in_ad', $sess_array_admin);
            }
            return 1;
        }
        else if($result_mg)
        {
            
            $sess_array_manager = array();
            foreach($result_mg as $row_mg)
            {
                $year=$row_mg->year;
                $pieces = explode(" ",$year); 
                $pieces_start_string=explode("/",$pieces[0]); 
                $pieces_end_string=explode("/",$pieces[1]); 
                $pieces_start[0]=(int)$pieces_start_string[0];
                $pieces_start[1]=(int)$pieces_start_string[1];
                $pieces_start[2]=(int)$pieces_start_string[2];
                $pieces_end[0]=(int)$pieces_end_string[0];
                $pieces_end[1]=(int)$pieces_end_string[1];
                $pieces_end[2]=(int)$pieces_end_string[2];
                $date = gmdate("Y-m-d-H-i-s", time()+7*3600);
                $date_str=  explode("-", $date);
                $year1=$date_str[0];
                $month1=$date_str[1];
                $day1=$date_str[2];
                if($pieces_end[0]<$year1)
                {
                    return 6;
                }
                else if($pieces_end[0]==$year1)
                {
                    if($pieces_end[1]<$month1)
                        return 6;
                    else if($pieces_end[1]==$month1)
                    {
                        if($pieces_end[2]<$day1)
                            return 6;
                        else
                        {
                            $sess_array_manager = array('id_mg' => $row_mg->id_mg);
                            $this->session->set_userdata('logged_in_mg', $sess_array_manager);
                            return 2;
                        }
                    }
                    else
                    {
                        $sess_array_manager = array('id_mg' => $row_mg->id_mg);
                        $this->session->set_userdata('logged_in_mg', $sess_array_manager);
                        return 2;
                    }
                }
                else 
                {
                    $sess_array_manager = array('id_mg' => $row_mg->id_mg);
                    $this->session->set_userdata('logged_in_mg', $sess_array_manager);
                    return 2;
                }
            }
        }
        else
        {
            //$this->form_validation->set_message('check_database', 'Invalid username or password');
            return 0;
        }
    }
    
}
?>